Lrn

执行局部响应归一化 (Local Response Normalization)。

\[\text{output}_{i,j} = \text{input}_{i,j} \times \left( \text{bias} + \alpha \sum_{k=\max(0, j-d)}^{\min(C-1, j+d)} (\text{input}_{i,k})^2 \right)^{-\beta}\]

其中,i 遍历每个 out_size 维度,j 表示当前通道,d 是 depth_radius,C 是总通道数 channel, :math:alpha 和 :math:beta 是缩放因子。

输入:
  • input - 输入张量数据地址,其逻辑布局为 (out_size, channel)。

  • params - 参数打包成数组:
    • out_size - 空间/批处理维度的乘积 (例如 N*H*W)。

    • channel - 通道数 (C)。

    • depth_radius - 归一化窗口的半径。总的窗口大小为 2 * depth_radius + 1。

    • *alpha - 缩放因子指针 :math:alpha。

    • *beta - 指数指针 :math:beta。

    • *bias - 偏置项指针。

  • core_mask - 核掩码(仅共享存储版本需要)。

输出:
  • Output - 计算结果地址。

支持平台:

FT78NE MT7004

备注

  • FT78NE 支持fp32

  • MT7004 支持fp16, fp32

共享存储版本:

void hp_lrn_s(half *input, half *output, long long *params, int core_mask)
void fp_lrn_s(float *input, float *output, long long *params, int core_mask)

C调用示例:

 1//FT78NE示例
 2#include <stdio.h>
 3#include <lrn.h> // 假设头文件名为 lrn.h
 4int main(int argc, char* argv[]) {
 5    float *input = (float *)0xA0000000;   //input在DDR空间
 6    float *output = (float *)0xC0000000;
 7    int out_size = 256; // 例如 N*H*W
 8    int channel = 96;
 9    int depth_radius = 5;
10    float alpha = 0.0001f;
11    float beta = 0.75f;
12    float bias = 1.0f;
13    int core_mask = 0xff;
14    long long params[8];
15    params[0] = (long long)out_size;
16    params[1] = (long long)channel;
17    params[2] = (long long)depth_radius;
18    params[3] = (long long)(&alpha);
19    params[4] = (long long)(&beta);
20    params[5] = (long long)(&bias);
21    params[6] = (long long)core_mask;
22    fp_lrn_s(input, output, params, core_mask);
23    return 0;
24}

私有存储版本:

void hp_lrn_p(half *input, half *output, long long *params)
void fp_lrn_p(float *input, float *output, long long *params)

C调用示例:

 1//FT78NE示例
 2#include <stdio.h>
 3#include <lrn.h> // 假设头文件名为 lrn.h
 4int main(int argc, char* argv[]) {
 5    float *input = (float *)0x10000000;   //input在L2空间
 6    float *output = (float *)0x10010000;
 7    int out_size = 256; // 例如 N*H*W
 8    int channel = 96;
 9    int depth_radius = 5;
10    float alpha = 0.0001f;
11    float beta = 0.75f;
12    float bias = 1.0f;
13    long long params[8];
14    params[0] = (long long)out_size;
15    params[1] = (long long)channel;
16    params[2] = (long long)depth_radius;
17    params[3] = (long long)(&alpha);
18    params[4] = (long long)(&beta);
19    params[5] = (long long)(&bias);
20    fp_lrn_p(input, output, params);
21    return 0;
22}